package simplescenegraph.profile;

import simplescenegraph.utils.ArrayTree;

public final class Chronometer {

	private static final class Routine {
		long beginTime, endTime;
		String name;

		Routine(long beginTime, String name) {
			this.beginTime = beginTime;
			this.name = name;
		}
		
		public String toString() {
			return "- " + name + ": " + (endTime - beginTime) + " ms";
		}
	}

	private static ArrayTree<Routine> routinesTree = new ArrayTree<Routine>();

	private static ArrayTree.Node<Routine> n0 = null;

	static {
		n0 = routinesTree.add(null, new Routine(System.nanoTime(), "logging: "));
	}

	private Chronometer() {
	}

	public static void start(String routineName) {
		n0 = routinesTree.add(n0, new Routine(System.nanoTime(), routineName));
	}

	public static long stop() {
		long l0;
		Routine r0 = n0.getData();
		r0.endTime = l0 = System.nanoTime();
		n0 = routinesTree.next(n0);
		if (n0 == null)
			n0 = routinesTree.getRoot();
		return l0;
	}

	public static void print() {
		if (n0 != null)
			stop();

		System.out.println(routinesTree.toString());
	}

}
